package com.wk.myplayer.util;

import android.content.Context;
import android.opengl.GLES20;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class WlShaderUtil {


    public static String readRawTxt(Context context, int rawId) {
        InputStream inputStream = context.getResources().openRawResource(rawId);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder sb = new StringBuilder();
        String line;
        try   {
            while((line = reader.readLine()) != null)  {
                sb.append(line).append("\n");
            }
            reader.close();
        }
        catch (Exception e)  {
            e.printStackTrace();
        }
        return sb.toString();
    }



    public static int createProgram(String vertexSource, String fragmentSource)  {
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
        if(vertexShader == 0)  {
            return 0;
        }

        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
        if(fragmentShader == 0)  {
            return 0;
        }

        int program = GLES20.glCreateProgram();
        if(program != 0) {
            GLES20.glAttachShader(program, vertexShader);// 将着色器加入到程序
            GLES20.glAttachShader(program, fragmentShader);
            GLES20.glLinkProgram(program);

            int[] linsStatus = new int[1];
            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linsStatus, 0);
            if(linsStatus[0] != GLES20.GL_TRUE){
                GLES20.glDeleteProgram(program);
                program = 0;
            }
        }
        return  program;
    }

    private static int loadShader(int shaderType, String source)  {
        int shader = GLES20.glCreateShader(shaderType);
        if(shader != 0)  {
            GLES20.glShaderSource(shader, source);
            GLES20.glCompileShader(shader);

            int[] compile = new int[1];
            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compile, 0);
            if(compile[0] != GLES20.GL_TRUE) {
                GLES20.glDeleteShader(shader);
                shader = 0;
            }
        }
        return shader;
    }

}
